package com.cloudcc.mobile.dao.impl;

import java.util.List;

import android.text.TextUtils;

import com.cloudcc.cloudframe.model.DynamicModel;
import com.cloudcc.cloudframe.net.async.EventRequest;
import com.cloudcc.mobile.AppConstant;
import com.cloudcc.mobile.dao.BaseEngine;
import com.cloudcc.mobile.dao.IBeauEngine;
import com.cloudcc.mobile.entity.beau.BeauInfoEntity;
import com.cloudcc.mobile.entity.beau.BeauListEntity;
import com.cloudcc.mobile.entity.beau.BeauListFilterEntity;
import com.cloudcc.mobile.entity.beau.BeauRelationFollowEntity;
import com.cloudcc.mobile.event.BeauEventList.BeauInfoEvent;
import com.cloudcc.mobile.event.BeauEventList.BeauListEvent;
import com.cloudcc.mobile.event.BeauEventList.BeauListFilterEvent;
import com.cloudcc.mobile.event.BeauEventList.DynamicListEvent;
import com.cloudcc.mobile.event.BeauEventList.RelateionFollowListEvent;
import com.cloudcc.mobile.manager.UrlManager;
import com.loopj.android.http.RequestParams;

public class BeanEngineImpl extends BaseEngine implements IBeauEngine {

	@Override
	public void serachBeauList(String viewId, int pageNum, String sortBy,
			boolean sortModel, String keyword, String expression,
			BeauListEvent event) {
		EventRequest<List<BeauListEntity>> eventRequest = new EventRequest<List<BeauListEntity>>() {
		};
		eventRequest.setEvent(event);
		RequestParams params = new RequestParams();
		addParams(params, "viewId", viewId);
		addParams(params, "pageNUM", String.valueOf(pageNum));
		addParams(params, "pageSize", String.valueOf(AppConstant.PAGE_SIZE));
		addParams(params, "sort", sortBy);
		addParams(params, "dir", sortModel ? "ASC" : "DESC");
		addParams(params, "viewId", viewId);
		addParams(params, "expression", expression);
		addParams(params, "keyword", keyword);
		sendPost(params, "searchByViewId", eventRequest);
	}

	/**
	 * 构建请求参数
	 * 
	 * @param params
	 * @param paramName
	 *            字段名
	 * @param paramValue
	 *            字段值
	 * @return
	 */
	private final void addParams(RequestParams params, String paramName,
			String paramValue) {
		if (params == null) {
			params = new RequestParams();
		}
		if (!TextUtils.isEmpty(paramValue)) {
			params.add(paramName, paramValue);
		}
	}

	@Override
	public void getFilter(String objectId, BeauListFilterEvent event) {
		EventRequest<BeauListFilterEntity> eventRequest = new EventRequest<BeauListFilterEntity>() {
		};
		eventRequest.setEvent(event);
		RequestParams params = new RequestParams();
		params.put("objectId", objectId);
		sendPost(params, "getQueryConditions", eventRequest);
	}

	@Override
	public void getBeauInfo(String id,String objApiName,String data, BeauInfoEvent event) {
		EventRequest<BeauInfoEntity> eventRequest = new EventRequest<BeauInfoEntity>() {};
		eventRequest.setEvent(event);
		RequestParams params = new RequestParams();
		params.put("recordId", id);
		params.put("objectApiName",objApiName);
		params.put("data",data);
		sendPost(params, "getFunctionMenusByRecordId", eventRequest);
	}

	@Override
	public void getBeauRelatDynamic(String id, DynamicListEvent event) {
		EventRequest<List<DynamicModel>> eventRequest = new EventRequest<List<DynamicModel>>() {
		};
		eventRequest.setEvent(event);
		sendGet(UrlManager.MOCK_URL + "beaudynamic.json", eventRequest);
	}

	@Override
	public void getBeauRelatFollows(Object data, RelateionFollowListEvent event) {
		EventRequest<BeauRelationFollowEntity> eventRequest = new EventRequest<BeauRelationFollowEntity>() {};
		eventRequest.setEvent(event);
		sendPost(data, eventRequest, "getRelationDataByRelationId", true);
	}

}
